package com.wondream.myframework.app.utils;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * User: rizenguo Date: 2014/11/1 Time: 14:06
 */
public class XMLParser {

    Logger logger = Logger.getLogger("XMLParser");

    public static Map<String, Object> getMapFromXML(String xmlString) {

        // 这里用Dom的方式解析回包的最主要目的是防止API新增回包字段
        //InputStream is = null;
        Map<String, Object> map = new HashMap<>(32);

        try (InputStream is = XMLUtil.getStringStream(xmlString)) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setXIncludeAware(false);
            factory.setExpandEntityReferences(false);
            DocumentBuilder builder = factory.newDocumentBuilder();
            //is = Util.getStringStream(xmlString);
            Document document = builder.parse(is);

            // 获取到document里面的全部结点
            NodeList allNodes = document.getFirstChild().getChildNodes();

            Node node;
            int i = 0;
            while (i < allNodes.getLength()) {
                node = allNodes.item(i);
                if (node instanceof Element) {
                    map.put(node.getNodeName(), node.getTextContent());
                }
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Test
    public void test() {
        String notifyContent = "<xml><appid><![CDATA[wx2cc328c53139c487]]></appid>\n" +
                "<attach><![CDATA[XCX]]></attach>\n" +
                "<bank_type><![CDATA[BOC_DEBIT]]></bank_type>\n" +
                "<cash_fee><![CDATA[11970]]></cash_fee>\n" +
                "<fee_type><![CDATA[CNY]]></fee_type>\n" +
                "<is_subscribe><![CDATA[N]]></is_subscribe>\n" +
                "<mch_id><![CDATA[1315513901]]></mch_id>\n" +
                "<nonce_str><![CDATA[8ouj9rq93bujfljljjnk4iug3rajws55]]></nonce_str>\n" +
                "<openid><![CDATA[oaTGws5Acvjkzqcv7PuiIOObhZyY]]></openid>\n" +
                "<out_trade_no><![CDATA[WX20210811235328238337]]></out_trade_no>\n" +
                "<result_code><![CDATA[SUCCESS]]></result_code>\n" +
                "<return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "<sign><![CDATA[743DBE91E7C2B26D669285F2CFD94D84]]></sign>\n" +
                "<sub_appid><![CDATA[wxc94daee5c5b654ad]]></sub_appid>\n" +
                "<sub_is_subscribe><![CDATA[N]]></sub_is_subscribe>\n" +
                "<sub_mch_id><![CDATA[1464098902]]></sub_mch_id>\n" +
                "<sub_openid><![CDATA[o6lYR0XhY1mY_0yZzXs74gfCzbXg]]></sub_openid>\n" +
                "<time_end><![CDATA[20210811235335]]></time_end>\n" +
                "<total_fee>11970</total_fee>\n" +
                "<trade_type><![CDATA[JSAPI]]></trade_type>\n" +
                "<transaction_id><![CDATA[4200001165202108113425606658]]></transaction_id>\n" +
                "</xml>";
        Map<String, Object> notifyMap = XMLParser.getMapFromXML(notifyContent);
        String returnCode = (String) notifyMap.get("return_code");
        logger.info(returnCode);
        if (StringUtils.equals("SUCCESS", returnCode)) {
            logger.info("订单支付成功");
        } else {
            logger.info("订单支付失败");
        }
    }
}